<template>
  <div id="System" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="数据字典" @newCreate="newCreate" @del="del"></v-tableTitle>
    <!-- <v-table :page="page" :pageSize="pageSize" :tableConfig="tableConfig" :tableData="tableData" :total="total" @findData="getTableData" @getTableData="getTableData" @searhData="searhData"
      @selectData="selectData" @setdialog="setdialog" @upData="getTableData" ref="table"></v-table> -->

      <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="`/querySystemCodeList/${$store.state.wms}/{page}`" :columnSource="tableConfig"
      @selection-change="data => tableSelectData = data" @setdialog="setdialog" />
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from 'commons/TableTitle'
import table from 'commons/Table'
import baseMixin from '@/mixins/BaseMixin'
import config from './Config'

export default {
  name: 'system',
  mixins: [baseMixin, config],
  data() {
    return {
      loading: false,
      tableConfig: [
        {
          label: this.$t('config.listName'),
          prop: 'listname',
          input: 'input'
        },
        {
          label: this.$t('common.desc'),
          prop: 'description',
          input: 'input'
        },
        {
          label: this.$t('config.parentlistName'),
          prop: 'parentlist',
          input: 'input'
        },
        {
          label: this.$t('config.parentCode'),
          prop: 'parentcode',
          input: 'input'
        }
      ],
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 0,
      dialogFlag: false,
      tableSelectData: [],
      tableSearhData: {}
    }
  },
  created() {},
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          certigier:
            this.newStatus ||
            (!this.btnPermNameList.includes('EN02050101') &&
              !this.btnPermNameList.includes('WM03080101')),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          certigier:
            this.deleteStatus ||
            (!this.btnPermNameList.includes('EN02050102') &&
              !this.btnPermNameList.includes('WM03080102')),
          disabled: true,
          invalid: 'del'
        }
      ]
    }
  },
  methods: {
    //新建
    newCreate() {
      this.$store.commit('setNewFlag', true)
      this.$router.push({ name: 'systemDeawer' })
    },
    //删除
    async del(data) {
      try {
        let res = await this.$api.post(
          `/deleteCodelist/${this.$store.state.wms}`,
          data
        )

        this.$message.success(res.returnMgr)
        let page = sessionStorage.getItem('page')
        this.getTableData(page, this.tableSearhData)
      } catch (e) {
        this.$message.error(e)
      }
    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data
    },
    async getTableData(page, data) {
      // 分页调用该对象验证
      if (data === undefined) {
        data = {}
      }
      try {
        this.loading = true
        let res = await this.$api.post(
          `/querySystemCodeList/${this.$store.state.wms}/${page}`,
          data
        )

        this.tableData = res.page.list
        this.page = res.page.curpage
        this.pageSize = res.page.pageSize
        this.total = res.page.total
      } catch (e) {
        this.$message.error(e)
      } finally {
        this.loading = false
      }
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data
      } else {
        this.tableSearhData = data
        this.getTableData(1, this.tableSearhData)
      }
    },
    //设置弹框
    setdialog(data) {
      this.$store.commit('setRouteData', data)
      this.$router.push({ name: 'systemDeawer' })
    }
  },
  mounted() {
    this.$store.commit('setSkip', {})
    this.$store.commit('setRouteData', {})
    this.$store.commit('setNewFlag', false)
    this.getTableData(1, {})
  },
  watch: {
    tableSelectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteStatus = false
        } else {
          this.deleteStatus = true
        }
      }
    }
  },
  components: {
    'v-tableTitle': tableTitle,
    'v-table': table
  }
}
</script>

<style scoped>
#System {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
